from pymongo import MongoClient
from bson.code import Code
client = MongoClient('mongodb://ecp:ecp123456@134.175.112.213:27017/ecp_db')
db = client['ecp_db']

result = db['product_list'].find({})
i = 0
for product in result:
  # try:
    # product = result.next()
    # if product is None:
    #   break
  if 'product_img' in product and 'product_price' in product and 'product_rating' in product and 'product_reviews' in product and product['product_price'] < 10 and product['product_rating'] > 3.5 and product['product_reviews'] < 1000:
    i = i + 1
    print("do result for " + str(i))
    aProduct = {
      "_id":product["_id"],
      "product_img" : product["product_img"],
      "product_name" : product["product_name"],
      "product_price" : product["product_price"],
      "product_url" : product["product_url"],
      "product_rating" : product["product_rating"],
      "product_reviews" : product["product_reviews"]
    }
  
    if "cate_ref" in product:
      aProduct["cate_ref"] = product["cate_ref"]
    if "cate_top" in product:
      aProduct["cate_top"] = product["cate_top"][0]
    if "bestsellers_rank" in product:
      for key in product["bestsellers_rank"].keys():
        aProduct["cate_ref_key"] = []
        aProduct["cate_ref_key"].append(key)
    db['a1_product'].update_one({'_id':product["_id"]},{'$set':aProduct},upsert=True)
  #except StopIteration:
    # 遇到StopIteration就退出循环
    #break

client.close()
print("finish...,totoal" + str(i))